<template>
  <div class="content">
    <div class="poster_type">
      <ul>
        <li :class="{ active: type == 1 }" @click="setType(1)">
          文章
          <i></i>
        </li>
        <li :class="{ active: type == 2 }" @click="setType(2)">
          海报
          <i></i>
        </li>
        <li :class="{ active: type == 3 }" @click="setType(3)">
          视频
          <i></i>
        </li>
        <li :class="{ active: type == 4 }" @click="setType(4)">
          活动
          <i></i>
        </li>
        <li :class="{ active: type == 5 }" @click="setType(5)">
          招聘
          <i></i>
        </li>
      </ul>
    </div>

    <div>
      <div class="search">
        <van-search
          shape="round"
          v-model="title"
          placeholder="请输入搜索关键词"
          @input="searchKey"
        />
      </div>
      <!-- 文章 -->
      <div class="main" v-if="type == 1 && articleArr.length !== 0">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          :immediate-check="false"
          @load="onLoad"
        >
          <div
            class="item"
            @click="$router.push(`/detail?id=${item.id}&from=share`)"
            v-for="(item, index) in articleArr"
            :key="index"
          >
            <div class="left">
              <img :src="$publicObj.urlImg(item.cover)" />
              <img
                src="../../assets/images/task/getMoney.png"
                alt=""
                class="money"
                v-show="item.type == 9"
              />
            </div>
            <div class="right">
              <span class="title">{{ item.title }}</span>
              <span class="p">{{ item.description }}</span>
              <div class="bottom">
                <span>转发:{{ item.share || 0 }}次</span>
                <span>阅读:{{ item.score || 0 }}次</span>
                <span>上传时间:{{ item.gmtCreate.substring(0, 11) }}</span>
              </div>
            </div>
          </div>
        </van-list>
      </div>
      <!-- 海报   -->
      <div class="main" v-else-if="type == 2 && posterArr.length !== 0">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <div
            class="item"
            v-for="item in posterArr"
            :key="item.id"
            @click="$router.push(`/postDetail?id=${item.id}&from=share`)"
          >
            <div class="left">
              <img :src="$publicObj.urlImg(item.cover)" />
              <img
                src="../../assets/images/task/getMoney.png"
                alt=""
                class="money"
                v-show="item.type == 9"
              />
            </div>
            <div class="right">
              <span class="title">{{ item.title }}</span>
              <span class="p">{{ item.description }}</span>
              <div class="bottom">
                <span>转发:{{ item.share || 0 }}次</span>
                <span>阅读:{{ item.score || 0 }}次</span>
                <span>上传时间:{{ item.gmtCreate.substring(0, 11) }}</span>
              </div>
            </div>
          </div>
        </van-list>
      </div>
      <!-- 视频 -->
      <div class="main" v-else-if="type == 3 && videoArr.length !== 0">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <div
            class="item"
            @click="$router.push(`/videoDetail?id=${item.id}&from=share`)"
            v-for="(item, index) in videoArr"
            :key="index"
          >
            <div class="left">
              <img :src="$publicObj.urlImg(item.cover)" />
              <img
                src="../../assets/images/task/getMoney.png"
                alt=""
                class="money"
                v-show="item.type == 9"
              />
            </div>
            <div class="right">
              <span class="title">{{ item.title }}</span>
              <span class="p">{{ item.description }}</span>
              <div class="bottom">
                <span>转发:{{ item.share || 0 }}次</span>
                <span>阅读:{{ item.score || 0 }}次</span>
                <span>上传时间:{{ item.gmtCreate.substring(0, 11) }}</span>
              </div>
            </div>
          </div>
        </van-list>
      </div>
      <!-- 活动 -->
      <div class="main" v-else-if="type == 4 && activityArr.length !== 0">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <div
            class="item"
            @click="$router.push(`/videoDetail?id=${item.id}&from=share`)"
            v-for="(item, index) in activityArr"
            :key="index"
          >
            <div class="left">
              <img :src="$publicObj.urlImg(item.cover)" />
              <img
                src="../../assets/images/task/getMoney.png"
                alt=""
                class="money"
                v-show="item.type == 9"
              />
            </div>
            <div class="right">
              <span class="title">{{ item.title }}</span>
              <span class="p">{{ item.description }}</span>
              <div class="bottom">
                <span>已分享:{{ item.share || 0 }}次</span>
                <span>上传时间:{{ item.gmtCreate }}</span>
              </div>
            </div>
          </div>
        </van-list>
      </div>
      <!-- 招聘 -->
      <div class="main" v-else-if="type == 5 && recruitmentArr.length !== 0">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <div
            class="item"
            @click="
              $router.push(`/recruitmentdetails?id=${item.id}&from=share`)
            "
            v-for="(item, index) in recruitmentArr"
            :key="index"
          >
            <div class="left">
              <img :src="$publicObj.urlImg(item.cover)" />
              <img
                src="../../assets/images/task/getMoney.png"
                alt=""
                class="money"
                v-show="item.type == 9"
              />
            </div>
            <div class="right">
              <span class="title">{{ item.title }}</span>
              <span class="p">{{ item.description }}</span>
              <div class="bottom">
                <span>已分享:{{ item.share || 0 }}次</span>
                <span>上传时间:{{ item.gmtCreate }}</span>
              </div>
            </div>
          </div>
        </van-list>
      </div>
      <van-empty description="暂无数据" v-else />
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: "", //关键字
      time: null,
      articleArr: [], //文章列表
      posterArr: [], //海报列表
      videoArr: [], //视频列表
      activityArr: [], //活动列表
      recruitmentArr: [], //招聘列表
      totalPage: "",
      agentId: "",
      wxUserInfo: {},
      type: 1,
      tabelType: 1,
      loading: false, //加载状态
      finished: false, //完成
      page: 1, //页数
      limit: 8, //条数
      totalPage: "", //总页数
    };
  },
  created() {
    document.querySelector("html").style.backgroundColor = "#fff";
    document.querySelector("body").style.backgroundColor = "#fff";
  },
  async mounted() {
    this.wxUserInfo = JSON.parse(sessionStorage.getItem("userWxData"));

    this.agentId = sessionStorage.getItem("agentId");

    this.getArticle();
  },

  methods: {
    //触底下一页
    onLoad() {
      if (this.page >= this.totalPage) {
        this.finished = true;
        return;
      }
      this.page++;

      this.setType(this.type);
      // 加载状态结束
      this.loading = false;
    },
    //搜素
    searchKey() {
      clearTimeout(this.time);
      this.time = setTimeout(async (_) => {
        this.setType(this.type);

        this.time = null;
      }, 700);
    },
    // 切换
    setType(n) {
      if (this.type != n) {
        this.taskArr = [];
        this.page = 1;
        this.totalPage = "";
      }
      this.type = n;
      if (n == 1) {
        this.getArticle();
      } else if (n == 2) {
        this.getPosterList();
      } else if (n == 3) {
        this.getVideoList();
      } else if (n == 4) {
        this.getActivityList();
      } else if (n == 5) {
        this.getRecruitmentList();
      }
    },
    //获取视频列表
    async getVideoList() {
      let res = await this.$api.getShareArticleSysList({
        title: this.title, //关键字
        page: this.page,
        limit: this.limit,
        userId: sessionStorage.getItem("agentId"),
        openId: this.wxUserInfo.openid,
        tableType: 4,
        type: 2,
      });
      if (res.code != 0) return;
      //判断是否是第一页
      if (this.page == 1) {
        this.videoArr = res.data.records || [];
      } else {
        this.videoArr.push(...res.data.records);
      }
      this.totalPage = res.data.pages;
    },
    //获取招聘列表
    async getRecruitmentList() {
      let res = await this.$api.getShareArticleSysList({
        title: this.title, //关键字
        page: this.page,
        limit: this.limit,
        userId: sessionStorage.getItem("agentId"),
        openId: this.wxUserInfo.openid,
        tableType: 15,
        type: 2,
      });
      if (res.code != 0) return;
      //判断是否是第一页
      if (this.page == 1) {
        this.recruitmentArr = res.data.records || [];
      } else {
        this.recruitmentArr.push(...res.data.records);
      }

      this.totalPage = res.data.pages;
    },
    //获取活动列表
    async getActivityList() {
      let res = await this.$api.getShareArticleSysList({
        title: this.title, //关键字
        page: this.page,
        limit: this.limit,
        userId: sessionStorage.getItem("agentId"),
        openId: this.wxUserInfo.openid,
        tableType: 2,
        type: 2,
      });
      if (res.code != 0) return;
      //判断是否是第一页
      if (this.page == 1) {
        this.activityArr = res.data.records || [];
      } else {
        this.activityArr.push(...res.data.records);
      }
      this.totalPage = res.data.pages;
    },
    //获取海报列表
    async getPosterList() {
      let res = await this.$api.getShareArticleSysList({
        title: this.title, //关键字
        page: this.page,
        limit: this.limit,
        userId: sessionStorage.getItem("agentId"),
        openId: this.wxUserInfo.openid,
        tableType: 3,
        type: 2,
      });
      //判断是否是第一页
      if (this.page == 1) {
        this.posterArr = res.data.records || [];
      } else {
        this.posterArr.push(...res.data.records);
      }
      this.totalPage = res.data.pages;
    },
    //获取文章列表
    async getArticle() {
      let res = await this.$api.getShareArticleSysList({
        title: this.title, //关键字
        page: this.page,
        limit: this.limit,
        userId: sessionStorage.getItem("agentId"),
        openId: this.wxUserInfo.openid,
        tableType: 1,
        type: 2,
      });
      if (res.code != 0) return;

      //判断是否是第一页
      if (this.page == 1) {
        this.articleArr = res.data.records || [];
      } else {
        this.articleArr.push(...res.data.records);
      }
      this.totalPage = res.data.pages;
    },
  },
};
</script>

<style scoped lang="less">
.poster_type {
  padding: 0 30px;
  background-color: #fff;
  ul {
    display: -webkit-flex;
    display: flex;
    justify-content: space-between;
    li {
      font-size: 16px;
      color: #666;
      display: -webkit-flex;
      display: flex;
      justify-content: center;
      position: relative;
      align-items: center;
      padding: 5px 0;
      i {
        width: 28px;
        height: 3px;

        border-radius: 3px;
        position: absolute;
        bottom: 0;
      }
    }
    .active {
      font-weight: 700;
      color: #000;
      i {
        background-color: #0264d1;
      }
    }
  }
}
.content {
  padding: 15px 15px 0;
  .main {
    height: calc(100vh - 150px);
    overflow-y: scroll;
    .item {
      display: flex;
      margin-top: 12px;
      .left {
        flex: 1;
        width: 90px;
        height: 90px;
        margin-right: 15px;
        position: relative;
        img {
          width: 90px;
          height: 90px;
          border-radius: 5px;
        }
        .money {
          width: 35px;
          height: 35px;
          position: absolute;
          right: 0;
          top: 0;
          border-radius: none;
        }
      }
      .right {
        flex: 3;
        span {
          font-size: 11px;
          color: #999999;
        }
        .title {
          display: block;
          font-size: 14px;
          color: #333333;
        }
        .p {
          margin-top: 3px;
          height: 30px;
          display: -webkit-box;
          overflow: hidden;
          -webkit-box-orient: vertical;
          text-overflow: ellipsis;
          -webkit-line-clamp: 2;
        }
        .bottom {
          width: 246px;
          margin-top: 8px;
          display: flex;
          justify-content: space-between;
        }
      }
    }
  }
  .main_two {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    height: calc(100vh - 150px);
    overflow-y: scroll;
    .item {
      margin-top: 14px;
      width: 48%;
      height: 165px;
      position: relative;
      img {
        width: 100%;
        height: 100%;
        border-radius: 5px;
      }
      .money {
        width: 35px;
        height: 35px;
        position: absolute;
        right: 0;
        top: 0;
        border-radius: none;
      }
      .share {
        position: absolute;
        bottom: 0;
        left: 0;
        height: 30px;
        width: 100%;
        background-color: rgba(90, 90, 90, 0.5);
        display: flex;
        align-items: center;
        justify-content: flex-end;
        span {
          margin-right: 10px;
          font-size: 10px;
          color: #f3f5f7;
        }
      }
    }
  }
}
</style>
